package TeacherEvaluation.com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import TeacherEvaluation.com.entity.Poll;
import TeacherEvaluation.com.entity.ResultMsg;
import TeacherEvaluation.com.entity.Score;
import TeacherEvaluation.com.entity.Student;
import TeacherEvaluation.com.entity.Teacher;
import TeacherEvaluation.com.entity.te_score;
import TeacherEvaluation.com.service.markService;
import TeacherEvaluation.com.service.pollService;
import TeacherEvaluation.com.service.scoreService;

@Controller
public class markController {
	@Autowired
	markService mService;
	@Autowired
	pollService pollService;
	@Autowired
	scoreService scoreService;
	
	@RequestMapping("/seleMark")
	ModelAndView SeleMark(String ma_type) {
		ModelAndView mView = new ModelAndView("admin/mark/showMark");
		mView.addObject("Mark", mService.selectMaByType(Integer.parseInt(ma_type)));
		return mView;
	}
	
	@RequestMapping("/BeginMark")
	ModelAndView BeginMark(String as_id) {
		ModelAndView mView = new ModelAndView("students/showMark");
		mView.addObject("as_id",as_id);
		mView.addObject("Mark", mService.selectMaByType(1));
		return mView;
	}
	
	@RequestMapping("/TeBeginMark")
	ModelAndView TeBeginMark(String te_id) {
		ModelAndView mView = new ModelAndView("teachers/showMark");
		mView.addObject("te_id",te_id);
		mView.addObject("Mark", mService.selectMaByType(0));
		return mView;
	}
	
	@ResponseBody
	@RequestMapping("/submitMark")
	ResultMsg submitMark(HttpServletRequest request, HttpServletResponse response,String ma_order,String as_id) {
		//获取student
		HttpSession session=request.getSession();
		
		Student user=(Student)session.getAttribute("user");
		
		String Mark="Mark";
		String ma_id="ma_id";
		String ma_score="ma_score";
		Double sum=0.0;
		
		for(int i=1;i<=Integer.parseInt(ma_order);i++) {
			ma_id=request.getParameter("ma_id"+i);
			Mark=request.getParameter("Mark"+i);
			ma_score=request.getParameter("ma_score"+i);
			Poll poll = new Poll();
			poll.setAs_id(Integer.parseInt(as_id));
			poll.setMa_id(Integer.parseInt(ma_id));
			System.out.println(Mark+" "+ma_id+" "+ma_score);
			if (Mark.equals("A")) {
				sum=sum+Double.valueOf(ma_score)*5;
				pollService.selectPoByAsMa(poll, "A");
			}else if (Mark.equals("B")) {
				sum=sum+Double.valueOf(ma_score)*3;
				pollService.selectPoByAsMa(poll, "B");
			}else if (Mark.equals("C")) {
				sum=sum+Double.valueOf(ma_score)*2;
				pollService.selectPoByAsMa(poll, "C");
			}else if (Mark.equals("D")) {
				sum=sum+Double.valueOf(ma_score)*1;
				pollService.selectPoByAsMa(poll, "D");
			}
		}
		sum=sum/5*100;
		Score score = new Score();
		score.setAs_id(Integer.parseInt(as_id));
		score.setStu_id(user.getId());
		score.setScore(sum);
		int i = scoreService.insertStuScore(score);
		if (i==1) {
			return new ResultMsg(1,"评教成功"+sum);
		}else {
			return new ResultMsg(0,"评教失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/submitTeMark")
	ResultMsg submitTeMark(HttpServletRequest request, HttpServletResponse response,String ma_order,String te_id) {
		//获取student
		HttpSession session=request.getSession();
		
		Teacher user=(Teacher)session.getAttribute("user");
		
		String Mark="Mark";
		String ma_id="ma_id";
		String ma_score="ma_score";
		Double sum=0.0;
		
		for(int i=1;i<=Integer.parseInt(ma_order);i++) {
			ma_id=request.getParameter("ma_id"+i);
			Mark=request.getParameter("Mark"+i);
			ma_score=request.getParameter("ma_score"+i);
			
			System.out.println(Mark+" "+ma_id+" "+ma_score);
			if (Mark.equals("A")) {
				sum=sum+Double.valueOf(ma_score)*5;
			}else if (Mark.equals("B")) {
				sum=sum+Double.valueOf(ma_score)*3;
			}else if (Mark.equals("C")) {
				sum=sum+Double.valueOf(ma_score)*2;
			}else if (Mark.equals("D")) {
				sum=sum+Double.valueOf(ma_score)*1;
			}
		}
		sum=sum/5*100;
		te_score tScore = new te_score();
		tScore.setTea_te_id(user.getTe_id());
		tScore.setTe_id(Integer.parseInt(te_id));
		tScore.setScore(sum);
		tScore.setTerm("2019上学期");
		int i = scoreService.insertTerScore(tScore);
		if (i==1) {
			return new ResultMsg(1,"评教成功"+sum);
		}else {
			return new ResultMsg(0,"评教失败");
		}
	}
}
